package indi.fritz.bigmap.volme;

import java.util.function.Consumer;

import indi.fritz.bigmap.common.Page;
import indi.fritz.bigmap.util.ByteUtil;
import indi.fritz.bigmap.volme.model.KeyValue;

public interface Engine extends AutoCloseable{
	
	public boolean exist(byte[] key);
	public byte[] read(byte[] key);
	public long write(byte[] key, byte[] value);
	public byte[] delete(byte[] key);
	
	public default boolean exist(String key){
		return this.exist(ByteUtil.stringToByteArray(key));
	}
	public default String read(String key){
		byte[] result = this.read(ByteUtil.stringToByteArray(key));
		if(result == null){ return null; }
		return ByteUtil.byteArrayToString(result);
	}
	public default long write(String key, String value){
		return this.write(ByteUtil.stringToByteArray(key), ByteUtil.stringToByteArray(value));
	}
	public default String delete(String key){
		byte[] result = this.delete(ByteUtil.stringToByteArray(key));
		if(result == null){ return null; }
		return ByteUtil.byteArrayToString(result);
	}
	
	public Page<KeyValue> read(long cursor, int pageSize);
	public long length();
	public void forEach(Consumer<KeyValue> action);
	public void clear();
	public void close();
}
